package com.fadadianqi.logistics.admin.net.handler;

import com.fadadianqi.logistics.admin.net.ChannelHandler;
import com.fadadianqi.logistics.admin.net.Packet;
import com.fadadianqi.logistics.admin.net.packet.SMSPacketAck;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.commons.SpringUtils;
import com.fadadianqi.logistics.customer.model.MsgQueue;
import com.fadadianqi.logistics.customer.service.MsgSendJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;

public class SMSAckHandler implements ChannelHandler<SMSPacketAck> {

    private static final Log log = LogFactory.getLog(SMSAckHandler.class);

    @Override
    public void process(SMSPacketAck packet) throws Exception {
        String json = new String(packet.getBodyBytes(), Packet.CHARSET);
        log.debug("#process# action:" + packet.getHead().getAction() + ", json:" + json);
        HashMap<String, Object> map = (HashMap<String, Object>) JSONProvider.toBean(json, HashMap.class);
        //String jobId = (String) map.get("jobId");
        String queueId = (String) map.get("queueId");
        //String customerId = (String) map.get("customerId");
        //String mobile = (String) map.get("mobile");
        int status = (Integer) map.get("status");
        MsgQueue queue = msgSendJob.getMsgQueueById(queueId);
        if (queue != null) {
            queue.setStatus(status);
            msgSendJob.updateQueue(queue);
        }
    }

    private MsgSendJob msgSendJob = (MsgSendJob) SpringUtils.getWebBean("msgSendJob");

}
